// store/storePermission.js
import { asyncRouterMap, constantRoutes } from '../../router'
import { Message } from 'element-ui'
import el from 'element-ui/src/locale/lang/el'
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.role) {
//     return roles.some(role => route.meta.role.indexOf(role) >= 0)
//   } else {
//     return true
//   }
// }

const permission = {
  state: {
    routers: constantRoutes,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      if (!state.routers.includes(routers)) {
        state.routers = constantRoutes.concat(routers)
      }
    }
  },
  actions: {
    generateRoutes({ commit }, roles) {
      return new Promise(resolve => {
        let accessedRoutes
        if (roles.includes('ROLE_ADMIN')) {
          // alert('你拥有管理员权限,静态加载路由')
          accessedRoutes = asyncRouterMap || []
          commit('SET_ROUTERS', accessedRoutes)
        }
        resolve(accessedRoutes)
      })
    }
  }
}

export default permission
